package com.imy.virtual.helper.compat;

import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.IBinder;
import android.os.IInterface;
import android.os.RemoteException;
import android.util.Log;

import com.imy.virtual.client.IVClient;
import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.helper.utils.VLog;

import com.imy.virtual.server.am.ProcessRecord;
import com.imy.virtual.tools.AndroidVersionConstant;

import java.util.ArrayList;
import java.util.List;

import mirror.android.app.IApplicationThread;
import mirror.android.app.ServiceStartArgs;
import mirror.android.content.res.CompatibilityInfo;

public class IApplicationThreadCompat {
  public static void scheduleCreateService(ProcessRecord appThread, IBinder token, ServiceInfo info, int processState) throws RemoteException {
    VLog.i("scheduleCreateService:" + VirtualCore.get().getProcessName());

    IApplicationThread.scheduleCreateService.call(appThread.appThread, token, info, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO.get(), processState);

  }

  public static void scheduleBindService(IInterface appThread, IBinder token, Intent intent, boolean rebind,
      int processState) throws RemoteException {

    IApplicationThread.scheduleBindService.call(appThread, token, intent, rebind, processState);
  }

  public static void scheduleUnbindService(IInterface appThread, IBinder token, Intent intent) throws RemoteException {
    IApplicationThread.scheduleUnbindService.call(appThread, token, intent);
  }

  public static void scheduleServiceArgs(ProcessRecord process, IBinder token, boolean taskRemoved,
      int startId, int flags, Intent args) throws RemoteException {

    if (Build.VERSION.SDK_INT >= 26) {
      List<Object> list = new ArrayList<>(1);
      Object serviceStartArg = ServiceStartArgs.ctor.newInstance(taskRemoved, startId, flags, args);
      list.add(serviceStartArg);
      IApplicationThread.scheduleServiceArgs.call(process.appThread, token, ParceledListSliceCompat.create(list));
    } else {
      IApplicationThread.scheduleServiceArgs.call(process.appThread, token, taskRemoved, startId, flags, args);
    }

  }

  public static void scheduleStopService(IInterface appThread, IBinder token) throws RemoteException {
    IApplicationThread.scheduleStopService.call(appThread, token);
  }
}
